package com.dfdchem.imd.kaiwu.fragment;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.dfdchem.imd.kaiwu.R;
import com.dfdchem.imd.kaiwu.activity.base.BaseActivityUM;
import com.dfdchem.imd.kaiwu.util.StringUtil;

import java.util.Map;
import java.util.Objects;
import java.util.Set;

public class CommonDialog extends DialogFragment implements View.OnClickListener {

    private static final String TAG = "CommonDialog";

    public static final int DialogStyle = 1; // 普通弹窗， content, [cancelText], [confirmText], [uri], [title]
    public static final int DialogStyleFullText = 2; // 全屏文本弹窗， content, [cancelText], [title]

    private BaseActivityUM activityUM;
    private Map<String, String> data;

    private TextView tvAlertDialogTitle;
    private TextView tvAlertDialogMessage;
    private Button btnAlertDialogNegative;
    private Button btnAlertDialogPositive;
    private View btnAlertDialogParent;

    private int type;
    private String title;
    private String content;
    private String cancelText;
    private String confirmText;
    private String url;

    public CommonDialog(BaseActivityUM activityUM, Map<String, String> data) {
        this.activityUM = activityUM;
        this.data = data;

        if (data != null) {
            Set<String> keys = data.keySet();
            if (keys.contains("type") && data.get("type") != null) {
                this.type = Integer.parseInt(Objects.requireNonNull(data.get("type")));
            }

            if (keys.contains("title")) {
                this.title = data.get("title");
            }

            if (keys.contains("content")) {
                this.content = data.get("content");
            }

            if (keys.contains("cancelText")) {
                this.cancelText = data.get("cancelText");
            }

            if (keys.contains("confirmText")) {
                this.confirmText = data.get("confirmText");
            }

            if (keys.contains("url")) {
                this.url = data.get("url");
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.alert_dialog, container);
        initView(view);
        initData();
        initEvent();
        return view;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);

        // request a window without the title
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        return dialog;
    }

    @Override
    public void onStart() {
        super.onStart();

        Dialog dialog = getDialog();
        if (dialog != null) {
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
    }

    public void initView(View view) {
        tvAlertDialogTitle = view.findViewById(R.id.tvAlertDialogTitle);
        tvAlertDialogMessage = view.findViewById(R.id.tvAlertDialogMessage);
        btnAlertDialogNegative = view.findViewById(R.id.btnAlertDialogNegative);
        btnAlertDialogPositive = view.findViewById(R.id.btnAlertDialogPositive);
        btnAlertDialogParent = view.findViewById(R.id.btnAlertDialogParent);
    }

    public void initData() {
        if (!StringUtil.isEmpty(title)) {
            tvAlertDialogTitle.setText(title);
        } else {
            tvAlertDialogTitle.setVisibility(View.GONE);
        }

        tvAlertDialogMessage.setText(content);

        if (!StringUtil.isEmpty(cancelText)) {
            btnAlertDialogNegative.setText(cancelText);
        } else {
            btnAlertDialogNegative.setVisibility(View.GONE);
        }

        if (!StringUtil.isEmpty(confirmText)) {
            btnAlertDialogPositive.setText(confirmText);
        } else if (type == 2) {
            // type等于1时默认有确定按钮
            btnAlertDialogPositive.setVisibility(View.GONE);
        }
    }

    public void initEvent() {
        btnAlertDialogNegative.setOnClickListener(this);
        btnAlertDialogPositive.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnAlertDialogNegative:
                dismiss();
                break;
            case R.id.btnAlertDialogPositive:
                if (!StringUtil.isEmpty(url)) {
                    activityUM.handleScheme(Uri.parse(url));
                } else {
                    dismiss();
                }
                break;
        }
    }

    @Override
    public void show(@NonNull FragmentManager manager, @Nullable String tag) {
        if (StringUtil.isEmpty(content)) {
            return;
        }
        super.show(manager, tag);
    }

}
